package com.ydd.base;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.View;

import com.ydd.R;
import com.ydd.constans.Constans;
import com.ydd.widegt.HeaderView;

/**
 * Created by tao on 17-3-11.
 */

public class BaseFragment extends Fragment {
    protected Activity mContext;
    protected String TAG;

    private HeaderView mHeaderView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TAG = getActivity().getComponentName().getClassName();
    }

    public void initHeaderView(String title, boolean showLeft, View rootView) {
        mHeaderView = (HeaderView) rootView.findViewById(R.id.header);
        mHeaderView.showLeft(showLeft);
        mHeaderView.showTitleView(title);
    }

    public void initHeaderView(String title, View rootView) {
        mHeaderView = (HeaderView) rootView.findViewById(R.id.header);
        mHeaderView.showTitleView(title);
    }

    /**
     * 判断是否有指定的权限
     */
    public boolean hasPermission(String... permissions) {
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(mContext, permission)
                    != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    /**
     * 申请指定的权限.
     */
    public void requestPermission(int code, String... permissions) {
        if (Build.VERSION.SDK_INT >= 23) {
            requestPermissions(permissions, code);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case Constans.WRITE_READ_EXTERNAL_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    doWriteSDCard();
                }
                break;
            case Constans.HARDWEAR_CAMERA_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    doOpenCamera();
                }
                break;
        }
    }

    public void doWriteSDCard() {

    }

    public void doOpenCamera() {

    }
}
